package org.ling.xuan.pannel;

import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.table.TableView;
import org.ling.xuan.dao.MarkFileDao;
import org.ling.xuan.dao.ProjectDao;
import org.ling.xuan.entity.mo.MarkFile;
import org.ling.xuan.entity.mo.ProjectSetting;
import org.ling.xuan.table.MarkFileTableModel;
import org.ling.xuan.util.LoggerUtil;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MarkFileListPanel extends JBPanel {
    private final boolean unAdd;
    private TableView<MarkFile> table;
    private JButton deleteButton;

    public MarkFileListPanel(List<MarkFile> markFileList, boolean unAdd) {
        this.unAdd = unAdd;
        initializeUI(markFileList);
    }

    private void initializeUI(List<MarkFile> markFileList) {
        this.setLayout(new BorderLayout());

        // 创建表格模型
        MarkFileTableModel tableModel = new MarkFileTableModel(markFileList);
        table = new TableView<>(tableModel);
        
        // 隐藏 id 列
        table.getColumnModel().getColumn(0).setWidth(0);
        table.getColumnModel().getColumn(0).setMinWidth(0);
        table.getColumnModel().getColumn(0).setMaxWidth(0);

        
        // 创建滚动面板，并将表格添加到滚动面板中
        JBScrollPane scrollPane = new JBScrollPane(table);

        // 创建删除按钮
        if (unAdd) {
            deleteButton = new JButton("unMark File");
        }
        deleteButton.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            if (selectedRow != -1) {
                MarkFileDao.delete((String) tableModel.getValueAt(selectedRow, 0));
                tableModel.removeRow(selectedRow);
            }
        });

        // 将滚动面板和删除按钮添加到面板中
        this.add(scrollPane, BorderLayout.CENTER);
        this.add(deleteButton, BorderLayout.SOUTH);
    }

    public List<String> getSelectIds() {
        List<String> ids = new ArrayList<>();
        int[] rowIndexes = table.getSelectedRows();
        for (int i = 0; i < rowIndexes.length; i++) {
            ids.add((String) table.getModel().getValueAt(i, 0));
        }
        return ids;
    }

    public void consoleSelectedFileInfo(Project project) {
        List<String> relativeRelationPaths = getSelectFiles(true);
        List<String> relativeUnRelationPaths = getSelectFiles(false);
        LoggerUtil.log(project, "---------------------文件信息 beg------------------------------");
        List<ProjectSetting> projectSettings = ProjectDao.listAll();
        if (relativeUnRelationPaths.size() > 0) {
            LoggerUtil.log(project, "---------------------修改文件-----------------------------");
            relativeUnRelationPaths.forEach(s -> {
                for (ProjectSetting ps : projectSettings) {
                    if (s.startsWith(ps.getLocalPre())) {
                        LoggerUtil.log(project, s.replace(ps.getLocalPre(), ps.getAddPre()));
                    }
                }
            });
        }
        if (relativeRelationPaths.size() > 0) {
            LoggerUtil.log(project, "---------------------关联文件-----------------------------");
            relativeRelationPaths.forEach(s -> {
                for (ProjectSetting ps : projectSettings) {
                    if (s.startsWith(ps.getLocalPre())) {
                        LoggerUtil.log(project, s.replace(ps.getLocalPre(), ps.getAddPre()));
                    }
                }
            });
        }
        LoggerUtil.log(project, "---------------------文件信息 end------------------------------");
    }

    public List<String> getSelectFiles(boolean relation) {
        List<String> relativeRelationPaths = new ArrayList<>();
        List<String> relativeUnRelationPaths = new ArrayList<>();
        int[] rowIndexes = table.getSelectedRows();
        for (int i = 0; i < rowIndexes.length; i++) {
            String path = (String) table.getModel().getValueAt(i, 4);
            if ("true".equals(String.valueOf(table.getModel().getValueAt(i, 3)))) {
                relativeRelationPaths.add(path);
            } else {
                relativeUnRelationPaths.add(path);
            }
        }
        Collections.sort(relativeRelationPaths);
        Collections.sort(relativeUnRelationPaths);
        if (relation) {
            return relativeRelationPaths;
        } else {
            return relativeUnRelationPaths;
        }
    }
}

